package com.fyales.android.util;

import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.util.HashMap;

/**
 * 设备工具类
 * @author fyales
 */
public class DevUtil {

    private static Context mContext;
    private static boolean isDebug;
    private static HashMap<String, Long> oldTimeMap = new HashMap<String, Long>();

    private static final String NOT_INITIALIZE_ERROR_STRING = DevUtil.class.getSimpleName()
            + " not initialize. Please run " + DevUtil.class.getSimpleName() + ".initialize() first !";

    public static void initialize(Context context) {
        mContext = context;
    }

    /**
     * 设置debug 一般情况下不要调用 主要用于测试
     * @param isDebug
     */
    public static void setDebug(boolean isDebug) {
        DevUtil.isDebug = isDebug;
    }

    /**
     * 时间调试信息，以yourName为tag，日志输出每步花费的时间。<br>
     * 线下版本才会输入调试信息，线上版本会自动关闭
     *
     * @param yourName 你的名字，避免与其他人统计区分
     * @param message
     * @throws Exception
     */
    public static void timePoint(String yourName, String message) {
        if (mContext == null) {
            throw new RuntimeException(NOT_INITIALIZE_ERROR_STRING);
        }

        if (isDebug) {
            Long currentTime = System.currentTimeMillis();
            Long oldTimeTemp = oldTimeMap.get(yourName);
            if (oldTimeTemp == null) {
                oldTimeTemp = System.currentTimeMillis();
            }
            Long durationTime = currentTime - oldTimeTemp;
            Log.v(yourName, message + " durationTime:" + durationTime + " - tag:" + yourName);
            oldTimeTemp = currentTime;
            oldTimeMap.put(yourName, oldTimeTemp);
        }
    }

    public static boolean isDebug() {
        if (mContext == null) {
            throw new RuntimeException(NOT_INITIALIZE_ERROR_STRING);
        }
        return isDebug;
    }

    /**
     * 判断是否模拟器。如果返回TRUE，则当前是模拟器
     *
     * @param context
     * @return
     */
    public static boolean isEmulator(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = tm.getDeviceId();
        if (imei == null || imei.equals("000000000000000")) {
            return true;
        }
        return false;
    }


    /**
     * 手机操作系统是否>=HoneycombMR1 level16 4.1
     *
     * @return
     */
    public static boolean hasJellyBean() {
        return Build.VERSION.SDK_INT >= 16;// Build.VERSION_CODES.JELLY_BEAN;
    }

    /**
     * 手机操作系统是否>=HoneycombMR1 level17 4.2
     *
     * @return
     */
    public static boolean hasJellyBean4_2() {
        return Build.VERSION.SDK_INT >= 17;// Build.VERSION_CODES.JELLY_BEAN;
    }

    /**
     * 开启StrickMode
     */
    public static void enableStrictMode() {

        StrictMode.ThreadPolicy.Builder threadPolicyBuilder =
                new StrictMode.ThreadPolicy.Builder()
                        .detectAll()
                        .penaltyLog();

        StrictMode.VmPolicy.Builder vmPolicyBuilder =
                new StrictMode.VmPolicy.Builder()
                        .detectAll()
                        .penaltyLog();

        StrictMode.setThreadPolicy(threadPolicyBuilder.build());
        StrictMode.setVmPolicy(vmPolicyBuilder.build());
    }

    /**
     * 关闭StrickMode
     */
    public static void disableStrictMode() {

        StrictMode.ThreadPolicy.Builder threadPolicyBuilder =
                new StrictMode.ThreadPolicy.Builder()
                        .permitAll()
                        .penaltyLog();

        StrictMode.setThreadPolicy(threadPolicyBuilder.build());
    }

    /**
     * 获取一个全局的Context
     *
     * @return
     */
    public static Context getContext() {
        if (mContext == null) {
            throw new RuntimeException(NOT_INITIALIZE_ERROR_STRING);
        }
        return mContext;
    }

    public static void d(String yourName, String message) {
        if (mContext == null) {
            throw new RuntimeException(NOT_INITIALIZE_ERROR_STRING);
        }

        if (isDebug) {
            Log.d(yourName, message + " - tag:" + yourName);
        }
    }

    public static void v(String yourName, String message) {
        if (mContext == null) {
            throw new RuntimeException(NOT_INITIALIZE_ERROR_STRING);
        }

        if (isDebug) {
            Log.v(yourName, message + " - tag:" + yourName);
        }
    }

    public static void w(String tag, String msg, Throwable tr) {
        if (mContext == null)
            throw new RuntimeException(NOT_INITIALIZE_ERROR_STRING);

        if (isDebug)
            Log.w(tag, msg + " - tag:" + tag, tr);
    }

}

